<?php

class Faculty extends FMIS_Controller
 {
	public function __construct() {
		parent::__construct();
        $this->load->database();
        $this->load->model('faculty_user_model');
        $this->load->model('faculty_profile_model');
        $this->load->model('faculty_rank_model');
        $this->load->model('faculty_degree_model');
        $this->load->model('faculty_status_model');
        $this->load->model('faculty_department_model');

	}

    public function search()
    {
        if($this->input->server('REQUEST_METHOD') === 'GET')
        {
            $faculty_profiles = $this->faculty_profile_model->getAll();
            $profiles = array();

            $count = 0;
            foreach($faculty_profiles as $faculty_profile)
            {

                $profileStatus = $this->faculty_status_model->getById($this->faculty_user_model->getById($faculty_profile['MemberID'])['Status'])['Description']; //TODO lagyan ng status, dapat nsa fmis_member_profile table hindi sa fmis_member table
                $profiles[$count] = array(
                    'profileID' => $faculty_profile['MemberID'],
                    'profileStatus' => $profileStatus,
                    'profileRank' => $this->faculty_rank_model->getById($faculty_profile['Rank']),
                    'profileDept' => $this->faculty_department_model->getById($faculty_profile['Department'])['Description'],
                    'profileSpecialty' => $faculty_profile['Specialty'],
                    'profileFirstName' => $faculty_profile['FirstName'],
                    'profileMiddleName' => $faculty_profile['MiddleName'],
                    'profileLastName' => $faculty_profile['LastName'],
                    'profileBirthDate' => $faculty_profile['Birthdate'],
                    'profileAddress' => $faculty_profile['Address']
                );
                $count++;
            }

            $depts = $this->faculty_department_model->getAll();
            $data = array(
                'page_title' => 'Search',
                'cb_name' => 'name_filter',
                'cb_dept' => '',
                'cb_spec' => '',
                'name' => '',
                'dept' => '',
                'spec' => '',
                'archive_search' => 'unarchive',
                'departments' => $depts,
                'profiles' => $profiles
            );
            $this->setup_view('faculty/search', $data);
            return;
        }
        else
        {
            $name = $this->input->post('name_filter');
            $dept = $this->input->post('dept_filter');
            $spec = $this->input->post('spec_filter');
            $cb_name = $this->input->post('cb_name_filter');
            $cb_dept = $this->input->post('cb_dept_filter');
            $cb_spec = $this->input->post('cb_spec_filter');
            $rad_arch = $this->input->post('rad_arch_filter');

            $name = $cb_name == 'name_filter' ? $name : '';
            $dept = $cb_dept == 'dept_filter' ? $dept : '';
            $spec = $cb_spec == 'spec_filter' ? $spec : '';

/*
            $data = array(

                'name' => $name,
                'dept' => $dept,
                'spec' => $spec,
                'cb_name' => $cb_name,
                'cb_dept' => $cb_dept,
                'cb_spec' => $cb_spec

            );

            $this->setup_view('asdfasdf',$data);
            return;
            //*/

            $faculty_profiles = $this->faculty_profile_model->search($name, $dept, $spec);
            //$faculty_profiles = $this->faculty_profile_model->getAll();
            $profiles = array();

            $count = 0;
            foreach($faculty_profiles as $faculty_profile)
            {
                $profiles[$count] = array(
                    'profileID' => $faculty_profile['MemberID'],
                    'profileStatus' => $this->faculty_user_model->getById($faculty_profile['MemberID']), //TODO lagyan ng status, dapat nsa fmis_member_profile table hindi sa fmis_member table
                    'profileRank' => $this->faculty_rank_model->getById($faculty_profile['Rank']),
                    'profileDept' => $this->faculty_department_model->getById($faculty_profile['Department'])['Description'],
                    'profileSpecialty' => $faculty_profile['Specialty'],
                    'profileFirstName' => $faculty_profile['FirstName'],
                    'profileMiddleName' => $faculty_profile['MiddleName'],
                    'profileLastName' => $faculty_profile['LastName'],
                    'profileBirthDate' => $faculty_profile['Birthdate'],
                    'profileAddress' => $faculty_profile['Address']
                );
                $count++;
            }

            $depts = $this->faculty_department_model->getAll();
            $data = array(
                'page_title' => 'Search',
                'name' => $name,
                'dept' => $dept,
                'spec' => $spec,
                'cb_name' => $cb_name,
                'cb_dept' => $cb_dept,
                'cb_spec' => $cb_spec,
                'archive_search' => $rad_arch,
                'departments' => $depts,
                'profiles' => $profiles
            );

            $this->setup_view('faculty/search', $data);
            return;
            //*/

        }
    }

    public function viewprofile($id = 0)
    {
        $profile = $this->faculty_profile_model->getById($id);
        $profileRank = $this->faculty_rank_model->getById($profile['Rank']);
        $profileRank = $profileRank == null ? 'not specified' : $profileRank['Description'];


        $data = array(
            'page_title' => $profile['FirstName'].'\'s Profile',
            'profileID' => $profile['MemberID'],
            'profileStatus' => $this->faculty_status_model->getById($this->faculty_user_model->getById($profile['MemberID'])['Status'])['Description'], //TODO lagyan ng status, dapat nsa fmis_member_profile table hindi sa fmis_member table
            'profileRank' => $profileRank,
            'profileDegree' => $this->faculty_degree_model->getById($profile['Degree'])['Description'],
            'profileSpecialty' => $profile['Specialty'],
            'profileFirstName' => $profile['FirstName'],
            'profileMiddleName' => $profile['MiddleName'],
            'profileLastName' => $profile['LastName'],
            'profileBirthDate' => date_format(date_create($profile['Birthdate']), 'm/d/Y'),
            'profileAddress' => $profile['Address'],
            'profileDepartment' => $profile['Description'],

            'tblStatus' => $this->faculty_status_model->getAll(),
            'tblDegree' => $this->faculty_degree_model->getAll(),
            'tblRank' => $this->faculty_rank_model->getAll(),
            'tblDepartment' => $this->faculty_department_model->getAll()
        );

        $this->setup_view('faculty/viewprofile',$data);

    }

    public function edit_member()
    {
        $fname   = $this->input->post('edit_fname');
        $mname   = $this->input->post('edit_mname');
        $lname   = $this->input->post('edit_lname');
        $bday    = $this->input->post('edit_birthdate');
        $address = $this->input->post('edit_address');
        $status  = $this->input->post('edit_status');
        $degree  = $this->input->post('edit_degree');
        $rank    = $this->input->post('edit_rank');
        $dept    = $this->input->post('edit_department');
        $specs   = $this->input->post('edit_special');

        $bday = date_format(date_create($bday), 'Y-m-d');

        $filename = $_FILES['upload_photo']['name'];
        $filetemp = $_FILES['upload_photo']['tmp_name'];
        $filetype = $_FILES['upload_photo']['type'];
        $filesize = $_FILES['upload_photo']['size'];
        $filepath = 'img/members/'.$this->session->userdata('user_id').'.jpg';

        move_uploaded_file($filetemp, $filepath);

        echo $filename;
        echo $filetemp;
        echo $filetype;
        echo $filesize;
        echo $filepath;

        //return;
        //*/


        $data = array(

            'fname'  => $fname   ,
            'mname'  =>$mname   ,
            'lname'  =>$lname   ,
            'bday'   =>$bday    ,
            'address'=>$address ,
            'status' =>$status  ,
            'degree' =>$degree  ,
            'rank'   =>$rank    ,
            'dept'   =>$dept    ,
            'specs'  =>$specs    ,
        );

        //$this->load->view('asdfasdf', $data);
        return json_encode($data);
        //return;
        //*/


        $this->faculty_profile_model->updateProfile($fname, $mname, $lname, $bday, $address, $degree,
            $rank, $dept, $specs, $this->session->userdata('user_id'));
        $this->faculty_user_model->updateStatus($this->session->userdata('user_id'), $status);

        $this->session->set_userdata(array('username' => $fname." ".$lname));


        redirect('profile');

    }

    public function editArchiveStatus($id = 0)
    {
        $profile = $this->faculty_profile_model->getById($id);
        $status = $this->faculty_user_model->getById($id)['Status'];

        $data = array(
            'id' => $id,
            'status' => $status,
            'firstname' => $profile['FirstName'],
            'middlename' => $profile['MiddleName'],
            'lastname' => $profile['LastName'],
        );
        echo $this->load->view('faculty/editArchiveStatus', $data, true);
    }

    public function changeArchiveStatus($id = 0)
    {
        $data = array();

        $status = $this->faculty_user_model->getById($id)['Status'];

        if($status == 1)
            $newstatus = 2;
        else
            $newstatus = 1;

        $this->faculty_user_model->updateStatusById($id, $newstatus);

        $data['newstatus'] = $newstatus;
        $data['status'] = 'Success';
        $data['message'] = 'Profile status updated';

        echo json_encode($data);
    }


    public function addprofile()
    {
        $data = array(
            'page_title' => 'Add Profile',
            'tblStatus' => $this->faculty_status_model->getAll(),
            'tblDegree' => $this->faculty_degree_model->getAll(),
            'tblRank' => $this->faculty_rank_model->getAll(),
            'tblDepartment' => $this->faculty_department_model->getAll()
        );
        $this->setup_view('faculty/addprofile', $data);
    }
}

/* End of file pages.php */
/* Location: ./application/controllers/pages.php */